﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL_NhaSach;
using BLL_NhaSach;

namespace AppQlyNhaSach.MatHang
{
    public partial class frmThemMH : DevExpress.XtraEditors.XtraForm
    {
        SachBLL sachBLL;
        TheLoaiBLL tlBLL;
        public frmThemMH()
        {
            InitializeComponent();
        }

        private void frmThemMH_Load(object sender, EventArgs e)
        {
            sachBLL = new SachBLL(Properties.Settings.Default.ConnectionStringUser);
            tlBLL=new TheLoaiBLL(Properties.Settings.Default.ConnectionStringUser);
            cboTheLoai.DataSource = tlBLL.getAllData();
            cboTheLoai.DisplayMember = "TenLoai";
            cboTheLoai.ValueMember = "MaLoai";
            cboTheLoai.SelectedIndex = -1;
            cboTheLoai.SelectedIndexChanged += new EventHandler(cboTheLoai_SelectedIndexChanged);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private String generateCode(String startCode)
        {
            Random rd = new Random();
            int code = rd.Next(100000, 10000000);
            return startCode + code.ToString();
        }
        private String getStartCode(String theloai)
        {
            String result = "";
            foreach (char c in theloai)
            {
                Boolean isUpper = Char.IsUpper(c);
                if (isUpper)
                    result += c;
            }
            return result;
        }

        private void cboTheLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            String name = tlBLL.getName(int.Parse(cboTheLoai.SelectedValue.ToString()));
            txtMa.Text = generateCode(getStartCode(name));
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMa.Text.Length > 0 && txtGiaBia.Text.Length > 0 && txtTacGia.Text.Length > 0 && txtTen.Text.Length > 0)
            {
                try
                {
                    decimal gb = decimal.Parse(txtGiaBia.Text);
                    DAL_NhaSach.Sach s = new DAL_NhaSach.Sach();
                    s.MaSach = txtMa.Text;
                    s.TenSach = txtTen.Text;
                    s.TacGia = txtTacGia.Text;
                    s.MaLoai = int.Parse(cboTheLoai.SelectedValue.ToString());
                    if (sachBLL.addObject(s))
                    {
                        XtraMessageBox.Show("Thêm mặt hàng thành công");
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    else XtraMessageBox.Show("Thêm thất bại");
                }
                catch (Exception)
                {
                    XtraMessageBox.Show("Giá bìa không hợp lệ");
                }
            }
            else XtraMessageBox.Show("Chưa nhập đầy đủ dữ liệu");
        }
    }
}